package com.hustzp.com.xichuangzhu.poetry.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.poetry.model.CollectionWorks;

import java.util.List;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/28 0:06
 * @Description:作品列表页适配器
 */
public class CollectionWorkAdapter extends BaseAdapter {
    private Context context;
    private List<CollectionWorks> collectionses;

    public CollectionWorkAdapter(Context context, List<CollectionWorks> collectionses) {
        this.context = context;
        this.collectionses = collectionses;
    }

    @Override
    public int getCount() {
        return collectionses.size();
    }

    @Override
    public CollectionWorks getItem(int position) {
        return collectionses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context,
                    R.layout.activity_category_list_item, null);
            holder.initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        bindViews(position, holder);
        return convertView;
    }

    private void bindViews(int position, ViewHolder holder) {
        CollectionWorks collections = getItem(position);
        holder.tv_title.setText(collections.getWork_title());
        holder.tv_dynasty.setText("[" + collections.getWork_dynasty() + "]");
        holder.tv_author.setText(collections.getWork_author());
        int endIndex = collections.getWork_content().indexOf("。") + 1;
        int wEndIndex = collections.getWork_content().indexOf("？") + 1;
        //遇到问号词,显示到问号位置
        if (wEndIndex != 0 && wEndIndex < endIndex) {
            endIndex = wEndIndex;
        }
        holder.tv_sub_title.setText(collections.getWork_content().substring(0, endIndex));
    }

    class ViewHolder {
        TextView tv_title;
        TextView tv_dynasty;
        TextView tv_author;
        TextView tv_sub_title;

        public void initView(View view) {
            tv_title = (TextView) view
                    .findViewById(R.id.tv_title);
            tv_dynasty = (TextView) view
                    .findViewById(R.id.tv_dynasty);
            tv_author = (TextView) view
                    .findViewById(R.id.tv_author);
            tv_sub_title = (TextView) view
                    .findViewById(R.id.tv_sub_title);

        }
    }
}
